export default {
  data () {
    return {
      time: '',
      timeId: '',
      currentTime: '',
      restTime: ''
    }
  },
  created () {
    this.timeId = setInterval(() => {
      this.setTime()
    }, 1000)
  },
  watch: {
    $route: {
      handler (val) {
        this.currentTime = new Date()
      },
      immediate: true
    }
  },
  beforeDestroy () {
    clearInterval(this.timeId)
  },
  methods: {
    // 设置时间
    setTime () {
      var date = new Date()
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      var hour = date.getHours()
      hour = this.turnDate(hour)
      var min = date.getMinutes()
      min = this.turnDate(min)
      var sec = date.getSeconds()
      sec = this.turnDate(sec)
      this.dat = year + '年' + month + '月' + day + '日'
      this.time = hour + ':' + min + ':' + sec
      this.restTime = parseInt(Math.round((10 * 60 * 1000 - (date - this.currentTime)) / 1000))
      if (this.restTime <= 0) {
        this.restTime = 0
        this.$refs.comp.init()
        this.currentTime = new Date()
      }
    },
    turnDate (date) {
      if (date < 10) {
        return date = '0' + date
      } else {
        return date
      }
    }
  }
}
